package com.bjsdzk.collector.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import com.bjsdzk.collector.entity.Temperature;

@Component
public class TemperatureDao {
    @Autowired
    private MongoTemplate mongoTemplate;
    
    /**
     * 保存一个Temperature信息到temperature集合里
     * @param stu
     */
    public void save(Temperature stu){
        try {
            mongoTemplate.save(stu,"temperature");
        } catch (Exception e) {
            // TODO: handle exception
        }    
    }
    
    /**
     * 查询所有的Temperature信息
     * @return
     */
    public List<Temperature> findAll(){
        return mongoTemplate.findAll(Temperature.class,"temperature");
    }
    
    /**
     * 通过Temperature.name查询document
     * @param name
     * @return
     */
    public List<Temperature> findByName(String name){
        List<Temperature> list=null;
        try {
            Query query=new Query(Criteria.where("name").is(name));
            list=mongoTemplate.find(query, Temperature.class,"temperature");
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
    
    /**
     * 通过Temperature.TemperatureScore.des查询document
     * @param des
     * @return
     */
    public List<Temperature> findByTemperatureScoreDes(String des){
        List<Temperature> list=null;
        try {
            Query query=new Query(Criteria.where("temperatureScore.des").is(des));
            list=mongoTemplate.find(query, Temperature.class,"temperature");
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
    
    
    /**
     * 通过Temperature.name和Temperature.des查询document
     *  @param name
     * @param des
     * @return
     */
    public List<Temperature> searchByNameAndDes(String name,String des){
        List<Temperature> list=null;
        try {
            Query query=new Query(Criteria.where("name").is(name).and("des").is(des));
            list=mongoTemplate.find(query, Temperature.class,"temperature");
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
    
    /**
     * 通过Temperature.name和Temperature.Temperature.des查询document
     *  @param name
     * @param des
     * @return
     */
    public List<Temperature> searchByNameAndTemperatureScoreDes(String name,String des){
        List<Temperature> list=null;
        try {
            Query query=new Query(Criteria.where("name").is(name).and("temperatureScore.des").is(des));
            list=mongoTemplate.find(query, Temperature.class,"temperature");
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
    
    /**
     * 更新
     * @param stu
     */
//    public void update(Temperature stu){
//        try {
//            Query query=new Query(Criteria.where("name").is(stu.getName()));
//            Update update= new Update().set("des", stu.getDes()).set("temperatureScore.des", stu.getTemperatureScore().getDes());
//            //更新查询返回结果集的第一条
//            mongoTemplate.updateFirst(query,update,Temperature.class,"temperature");
//            //更新查询返回结果集的所有
//            // mongoTemplate.updateMulti(query,update,Temperature.class);
//        } catch (Exception e) {
//            // TODO: handle exception
//        }
//    }
//    
    /**
     * 删除
     * @param name
     */
    public void remove(String name){
        Query query=new Query(Criteria.where("name").is(name));
        mongoTemplate.remove(query, Temperature.class,"temperature");
    }
}
